package leetcode;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Liangyaoyun
 */
public class leetCode47 {
    public List<List<Integer>>res=new ArrayList<>();
    public List<Integer>path=new ArrayList<>();


    public List<List<Integer>> permuteUnique(int[] nums) {
        boolean[] used=new boolean[nums.length];
        Arrays.sort(nums);
        dfs(nums,used,0);
        return res;
    }
    public void dfs(int[] nums,boolean[] used,int index){
        if (index==nums.length){
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (used[i]) {
                continue;
            }
            //以[1,2,2]为例,当已经有[1,2,2]时,在[1]这个位置如果再次遇到2,则进行剪枝操作
            if (i>0&&nums[i]==nums[i-1]&&used[i-1]==false){
                continue;
            }
            path.add(nums[i]);
            used[i]=true;
            dfs(nums,used,index+1);
            used[i]=false;
            path.remove(path.size()-1);
        }
    }
}
